<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.sycit.cn
// +----------------------------------------------------------------------
// | Author: Peter.Zhang  <hyzwd@outlook.com>
// +----------------------------------------------------------------------
// | Date:   2017/9/11
// +----------------------------------------------------------------------
// | Title:  Logistics.php
// +----------------------------------------------------------------------
namespace app\index\controller;

use app\index\model\Logistics AS LogisticsModel;
use think\Db;
use think\Url;
use think\Request;
use think\Session;

class Logistics extends Common_base
{

    public function _initialize()
    {
        // 是否有权限
        IS_ROOT([1,2])  ? true : $this->error('没有权限');
        return parent::_initialize(); // TODO: Change the autogenerated stub
    }

    public function index() {
        $Request = Request::instance();
        $query = $Request->param(); // 分页查询传参数
        $q = $Request->param('q');
        $model = new LogisticsModel();
        if ($q !== null) {
            $data = $model->where('log_name', 'like', '%'.$q.'%')->where('status', 1)->order('log_id', 'desc')->paginate('', false, ['query' => $query ]);
        } else {
            $data = $model->where('status', 1)->order('log_id', 'desc')->paginate(20);
        }
        // 获取分页显示
        $page = $data->render();
        $assign = [
            'title' => '物流信息',
            'data' => $data,
            'page' => $page,
            'empty' => '<tr><td colspan="7" align="center">当前条件没有查到数据</td></tr>',
        ];
        $this->assign($assign);
        return $this->fetch();
    }

    //增加物流信息
    public function add() {
        return $this->fetch();
    }

    //提交
    public function add_do() {
        $Request = Request::instance();
        if ($Request->isPost()) {
            $name = $Request->param('log_name');
            //查询是否已有名称存在
            if (LogisticsModel::getByLogName($name)) {
                $this->error('物流名称重复，请更换！');
            }
            $model = new LogisticsModel($_POST);
            $result = $model->allowField(true)->save();
            if ($result) {
                $this->success('新增物流信息成功');
            } else {
                $this->error('数据操作错误，请联系管理员！');
            }
        }
    }

    //修改
    public function edit() {
        $Request = Request::instance();
        $id = $Request->param('id');
        if (empty($id)) {
            $this->error('参数错误！');
        }
        if (!LogisticsModel::get($id)) {
            $this->error('没有数据！');
        }
        $model = new LogisticsModel();
        $data = $model->where('log_id', $id)->find();
        $this->assign('title', '修改物流信息');
        $this->assign('data', $data);
        return $this->fetch();
    }

    //提交修改
    public function edit_do() {
        $Request = Request::instance();
        if ($Request->isPost()) {
            $id = $Request->param('log_id');
            if (empty($id)) {
                $this->error('参数错误！');
            }
            if (!LogisticsModel::get($id)) {
                $this->error('没有数据！');
            }
            Db::name('logistics')->where('log_id', $id)->update([
                'log_phone' => $Request->param('log_phone'),
                'log_fax' => $Request->param('log_fax'),
                'log_address' => $Request->param('log_address'),
            ]);
            $this->success('修改成功', Url::build('logistics/index'));
        }
    }

    //查物流名称
    public function check_name() {
        $Request = Request::instance();
        if ($Request->isPost()) {
            $name = $Request->param('name');
            if (empty($name)) {
                $this->error('参数错误！');
            }
            $ByName = Db::name('logistics')->where('log_name', $name)->find();
            if ($ByName) {
                return false;
            } else {
                return true;
            }
        }
    }

    //选择物流
    public function select() {
        $Request = Request::instance();
        $query = $Request->param(); // 分页查询传参数
        $q = $Request->param('q');
        $model = new LogisticsModel();
        if ($q === null) {
            $data = $model->where('status', 1)->order('log_id', 'desc')->paginate(10);

        } else {
            $data = $model->where('log_name', 'like', '%'.$q.'%')->where('status', 1)->order('log_id', 'desc')->paginate('10', false, ['query' => $query ]);
        }
        // 获取分页显示
        $page = $data->render();
        $assign = [
            'data' => $data,
            'page' => $page,
            'empty' => '<tr><td colspan="5" align="center">当前条件没有查到数据</td></tr>',
        ];
        $this->assign($assign);
        return $this->fetch();
    }

    //删除
    public function delete() {
        // 设定数据返回格式
        \think\Config::set("default_return_type","json");
        $Request = Request::instance();
        if ($Request->isPost()) {
            $uid = $Request->param("uid");
            $name = $Request->param("name");
            if (empty($uid)) {
                $this->error('传入参数错误');
            }
            if ($name == 'delone') {
                // 单条删除操作
                Db::name('logistics')->where('log_id', $uid)->update(['status'=>'-1']);
                $this->success('删除成功', Url::build('logistics/index'));
            } elseif ($name == 'delallattr') {
                // 多条删除操作
                $arrUid = explode(",",$uid);
                if (!empty($arrUid)) {
                    $i=0;
                    foreach ($arrUid as $key=>$val) {
                        Db::name('logistics')->where('log_id', $val)->update(['status'=>'-1']);
                        $i++;
                    }
                    $this->success($i.' 条记录删除成功', Url::build('logistics/index'));
                }
            } else {
                // 不执行操作
                $this->error('传入参数错误');
            }
        }
    }
}